<?php

/**
 * This is the model class for table "{{book}}".
 *
 * The followings are the available columns in table '{{book}}':
 * @property string $book_id
 * @property string $book_category
 * @property string $book_category_name
 * @property string $book_title
 * @property string $book_desc
 * @property string $book_author
 * @property string $book_image
 * @property string $book_url
 * @property string $book_length
 * @property string $book_vote
 * @property integer $book_status
 * @property string $book_finish_time
 */
class Book extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Book the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{book}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('book_title', 'required', 'message'=>'小说标题不能为空'),
			array('book_title', 'unique', 'message'=>'小说标题已存在'),
			array('book_vote','numerical','message'=>'小说投票数必须为数字'),
			array('book_length','numerical','message'=>'小说总长度必须为数字'),
			array('book_status','numerical','message'=>'小说状态必须为数字'),
			array('book_image','file','allowEmpty'=>true,'types'=>'jpg,gif,png'),
			array('book_category','safe'),
			array('book_author','safe'),
			array('book_finish_time','safe'),
			array('book_category_name','safe'),
			array('book_url','safe'),
			array('book_desc','safe'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'book_id' => 'Book',
			'book_category' => 'Book Category',
			'book_category_name' => 'Book Category Name',
			'book_title' => 'Book Title',
			'book_desc' => 'Book Desc',
			'book_author' => 'Book Author',
			'book_image' => 'Book Image',
			'book_url' => 'Book Url',
			'book_length' => 'Book Length',
			'book_vote' => 'Book Vote',
			'book_status' => 'Book Status',
			'book_finish_time' => 'Book Finish Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('book_id',$this->book_id,true);
		$criteria->compare('book_category',$this->book_category,true);
		$criteria->compare('book_category_name',$this->book_category_name,true);
		$criteria->compare('book_title',$this->book_title,true);
		$criteria->compare('book_desc',$this->book_desc,true);
		$criteria->compare('book_author',$this->book_author,true);
		$criteria->compare('book_image',$this->book_image,true);
		$criteria->compare('book_url',$this->book_url,true);
		$criteria->compare('book_length',$this->book_length,true);
		$criteria->compare('book_vote',$this->book_vote,true);
		$criteria->compare('book_status',$this->book_status);
		$criteria->compare('book_finish_time',$this->book_finish_time,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	
	public function beforeSave()
	{
		//上传图片并保存图片路径
		$file = CUploadedFile::getInstance($this, 'book_image');
		if ($file)
		{
			$this->book_image = uniqid().'.'.$file->extensionName;
			$path = Yii::app()->basePath.'/../uploads/book_images/'.$this->book_image;
			$file->saveAs($path);
			Yii::import('application.extensions.resizeImage');
			$image = new resizeImage($path);
			$image->resize(111, 161);
			$image->save($path);
		}
		//获取文章分类名称
		$this->book_category_name = Category::model()->findByPk($this->book_category)->category_name;
		//转换完结时间为时间戳
		$this->book_finish_time = strtotime($this->book_finish_time);
		return parent::beforeSave();
	}
	
}